﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using OpenSunTracker.RestClient;
using OpenSunTracker.RestClient.Extensions;

namespace OSTWin
{
    /// <summary>
    /// Interaction logic for ucTracker.xaml
    /// </summary>
    public partial class ucTracker : UserControl
    {
        public ucTracker()
        {
            InitializeComponent();
        }

        public void UpdateVisuals()
        {
            if (!Client.HasState)
                return;

            if (Client.State.TrackingState.Contains("fine"))//tracker started
            {
                btnStart.IsEnabled = false;
                btnStop.IsEnabled = true;
            }
            else
            {
                btnStart.IsEnabled = true;
                btnStop.IsEnabled = false;
            }
        }

        public OpenSunTrackerClient Client { get { return DataContext as OpenSunTrackerClient; } }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            var actionUrl = Client.Url + "/tracker/start";
            try
            {
                var response = actionUrl.ExecuteReadRequest();
                Client.AddMessage("tracker-start (" + actionUrl + "): " + response.ReadAllText());
            }
            catch (Exception ex)
            {
                Client.AddMessage("error on tracker-start (" + actionUrl + "): " + ex.Message);
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            var actionUrl = Client.Url + "/tracker/stop";
            try
            {
                var response = actionUrl.ExecuteReadRequest();
                Client.AddMessage("tracker-stop (" + actionUrl + "): " + response.ReadAllText());
            }
            catch (Exception ex)
            {
                Client.AddMessage("error on tracker-stop (" + actionUrl + "): " + ex.Message);
            }
        }


    }
}
